﻿@inherits AdminCompontentBase
@page "/alarmHistory"
@page "/alarmHistory/{AlarmRuleId}"
<PageTitle>@T("AlarmHistory")</PageTitle>
<AutoHeight>
    <HeaderContent>
        <div class="d-flex">
            <SLabeledRadioGroup @bind-Value="@_queryParam.SearchType" ActiveClass="fill-background" Class="white" Dense OnChange="HandleSearchTypeChange">
                @foreach (var item in Enum.GetValues<AlarmHistorySearchTypes>())
                {
                    <SLabeledRadio Value="item" Class="px-6">
                        <span class="@(_queryParam.SearchType == item ? "subtitle2 emphasis2--text" : "btn regular2--text")">@T(item.ToString())</span>
                    </SLabeledRadio>
                }
            </SLabeledRadioGroup>
            <MSpacer></MSpacer>
            <div class="d-flex">
                <SSearch @bind-Value="_queryParam.Filter" BackgroundColor="white" OnEnter="HandleFilterKeyDown" OnClearClick="HandleClearAsync" />
                <div @onclick="ToggleAdvanced" class="ml-6 d-flex button fill">
                    <SIcon Class="ma-auto" Color="emphasis2" Size=20>
                        @(!advanced ? "mdi-filter" : "mdi-filter-off")
                    </SIcon>
                </div>
            </div>
        </div>
        @if (advanced)
        {
            <MRow Class="@($"mt-6 flex-none {(isAnimate?"animate__animated":"")} {(!advanced?"animate__fadeOut":"animate__fadeIn")}")" NoGutters Style="width:110%">
                <MCol Md="1">
                    <SSelect Small @bind-Value="@_queryParam.TimeType"
                         Items="_timeTypeItems"
                         Label="@T(nameof(_queryParam.TimeType))"
                         ItemText="e => T(e==default?string.Empty:e.ToString())"
                         ItemValue="e=> e"
                         BackgroundColor="white"
                         Clearable
                         TItem="AlarmHistorySearchTimeTypes"
                         TItemValue="AlarmHistorySearchTimeTypes"
                         TValue="AlarmHistorySearchTimeTypes"
                         Class="fill-card body2"
                         OnSelectedItemUpdate="RefreshAsync"
                         OnClearClick="RefreshAsync">
                    </SSelect>
                </MCol>
                <SDateTimeRangeToolbar Class="ml-6" StartDateTime="_queryParam.StartTime" EndDateTime="_queryParam.EndTime" OnUpdate="DateRangChangedAsync" ShowQuickChange="false" />
                <MCol Md="1" Class="ml-6">
                    <SSelect Small @bind-Value="@_queryParam.AlertSeverity"
                         Items="Enum.GetValues<AlertSeverity>().ToList()"
                         Label="@T(nameof(_queryParam.AlertSeverity))"
                         ItemText="e => T(e==default?string.Empty:e.ToString())"
                         ItemValue="e=> e"
                         BackgroundColor="white"
                         Clearable
                         TItem="AlertSeverity"
                         TItemValue="AlertSeverity"
                         TValue="AlertSeverity"
                         Class="fill-card body2"
                         OnSelectedItemUpdate="RefreshAsync"
                         OnClearClick="RefreshAsync">
                    </SSelect>
                </MCol>
                @if (_queryParam.SearchType == AlarmHistorySearchTypes.Alarming)
                {
                    <MCol Md="1" Class="ml-6">
                        <SSelect Small @bind-Value="@_queryParam.HandleStatus"
                         Items="_handleStatusItems"
                         Label="@T(nameof(_queryParam.HandleStatus))"
                         ItemText="e => T(e==default?string.Empty:e.ToString())"
                         ItemValue="e=> e"
                         BackgroundColor="white"
                         Clearable
                         TItem="AlarmHistoryHandleStatuses"
                         TItemValue="AlarmHistoryHandleStatuses"
                         TValue="AlarmHistoryHandleStatuses"
                         Class="fill-card body2"
                         OnSelectedItemUpdate="RefreshAsync"
                         OnClearClick="RefreshAsync">
                        </SSelect>
                    </MCol>
                }
                @if (_queryParam.SearchType == AlarmHistorySearchTypes.Processed)
                {
                    <MCol Md="2" Class="ml-6">
                        <SUserAutoComplete Small @bind-Value="_queryParam.Handler" Label="@(T("Handler"))" FillBackground="false" OnSelectedItemUpdate="RefreshAsync" Clearable />
                    </MCol>
                }
            </MRow>
        }
    </HeaderContent>
    <AutoHeightContent>
        <MCard Class="@(!advanced?"page-content mt-6":"page-content-advanced mt-6")">
            <SDataTable Headers="_headers" Items="_entities.Result" TItem="AlarmHistoryListViewModel" ItemsPerPage="_queryParam.PageSize" OnOptionsUpdate="HandleOnOptionsUpdate" HideDefaultFooter @ref="_dataTableRef" Class="d-flex full-height flex-column">
                <HeaderColContent Context="header">
                    <span class="text-btn">@header.Text</span>
                </HeaderColContent>
                <ItemColContent>
                    @switch (context.Header.Value)
                    {
                        case nameof(AlarmHistoryListViewModel.AlertSeverity):
                            <AlertSeverityTag Value="@context.Item.AlertSeverity" />
                            break;
                        case nameof(AlarmHistoryListViewModel.HandleStatus):
                            <AlarmHistoryStatusTag Value="@context.Item.HandleStatus" />
                            break;
                        case "HandleCompletionTime":
                            <MListItem Class="pa-0">
                                <MListItemContent>
                                    <MListItemTitle Class="body regular--text">@context.Item.Handle.HandlerName @context.Item.RecoveryTime?.ToOffset(JsInitVariables.TimezoneOffset).ToString(T("$TimeFormat"))</MListItemTitle>
                                    <MListItemSubtitle Class="body regular--text">@context.Item.RecoveryTime?.ToOffset(JsInitVariables.TimezoneOffset).ToString(T("$DateFormat"))</MListItemSubtitle>
                                </MListItemContent>
                            </MListItem>
                            break;
                        case "Action":
                            @if (context.Item.HandleStatus == AlarmHistoryHandleStatuses.Pending && _queryParam.SearchType!= AlarmHistorySearchTypes.NoNotice)
                            {
                                <MButton Class="mr-4" Icon OnClick="()=>_handleAlarmModal?.OpenModalAsync(context.Item)!">
                                    <SIcon Size="20" Small Color="emphasis2" Tooltip="@(T("Handle"))">mdi-account-check</SIcon>
                                </MButton>
                            }
                            else
                            {
                                <MButton Class="mr-4" Icon OnClick="()=>_detailModal?.OpenModalAsync(context.Item)!">
                                    <SIcon Size="20" Small Color="emphasis2" Tooltip="@(T("View"))">mdi-eye</SIcon>
                                </MButton>
                            }
                            break;
                        default:
                            <DefaultGenericColumnRender Value="@context.Value" Width="@context.Header.Width"></DefaultGenericColumnRender>
                            break;
                    }
                </ItemColContent>
                <FooterContent>
                    <SPagination Class="mt-3 mx-6 pb-6" Page="@_queryParam.Page" PageSize="@_queryParam.PageSize" Total="_entities.Total" PageChanged="HandlePageChanged" PageSizeChanged="HandlePageSizeChanged" />
                </FooterContent>
            </SDataTable>
        </MCard>
    </AutoHeightContent>
</AutoHeight>

<AlarmHistoryDetailModal @ref="_detailModal" OnOk="HandleOk"/>
<HandleAlarmModal @ref="_handleAlarmModal" OnOk="HandleOk"/>